package org.linlinjava.litemall.db.util;

import java.math.BigDecimal;

public class BigDecimalUtil {
    
    public static boolean smallerThan(BigDecimal x, int y) {
        BigDecimal b = new BigDecimal(y);
        return x.compareTo(b) < 0;
    }

    public static boolean biggerThan(BigDecimal x, int y) {
        BigDecimal b = new BigDecimal(y);
        return x.compareTo(b) > 0;
    }

    public static boolean equalTo(BigDecimal x, int y) {
        BigDecimal b = new BigDecimal(y);
        return x.compareTo(b) == 0;
    }

    public static boolean smallerThanOrEqualTo(BigDecimal x, int y) {
        return smallerThan(x, y) || equalTo(x, y);
    }

    public static boolean biggerThanOrEqualTo(BigDecimal x, int y) {
        return biggerThan(x, y) || equalTo(x, y);
    }


    /**
     * 提供（相对）精确的除法运算。当发生除不尽的情况时，由scale参数指
     * 定精度，以后的数字四舍五入。
     * @param v1 被除数
     * @param v2 除数
     * @param scale 表示表示需要精确到小数点以后几位。
     * @return 两个参数的商
     */
    public static double div(double v1,double v2,int scale){
        if(scale<0){
            throw new IllegalArgumentException(
                    "The scale must be a positive integer or zero");
        }
        if (v1 == 0) {
            return 0;
        }
        if (v2 == 0) {
            throw new IllegalArgumentException(
                    "您不能除灵");
        }
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        double hrwh = b2.divide(b1,scale,BigDecimal.ROUND_HALF_UP).doubleValue();
        System.out.println(hrwh);
        return b2.divide(b1,scale,BigDecimal.ROUND_HALF_UP).doubleValue();

    }
}
